//
//  ImageUtil.m
//  LOU
//
//  Created by iMacOne on 15/11/11.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "ImageUtil.h"
#import "FileUtil.h"
#import "DateUtil.h"
@implementation ImageUtil
#pragma mark 保持网络图片至本地
+ (NSString*)saveImageLocal:(NSData*)data
{
    NSString * fileName = [NSString stringWithFormat:@"%@%d.png",[DateUtil formatDateNoStr:[NSDate date]],arc4random_uniform(10000)];
    NSString * filePath = [FileUtil documentPath:fileName];
    [data writeToFile:filePath atomically:YES];
    return filePath;
}
//对图片尺寸进行压缩--
+ (UIImage*)reduce:(UIImage*)image size:(CGSize)size isAdjust:(BOOL)isAdjust
{
    if (image == nil) {
        return nil;
    }
    if (image.size.width < size.width && image.size.height < size.height && isAdjust) {
        return image;
    }
    float sx = size.width / image.size.width, sy = size.height / image.size.height;
    if (isAdjust) {
        sx = (sx < sy ? sx : sy);
        sy = sx;
    }
    CGSize newSize = CGSizeMake(image.size.width * sx, image.size.height * sy);
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
// 根据view截图
+ (UIImage*)cutView:(UIView*)view
{
    return [ImageUtil cutView:view size:view.bounds.size];
}

// 根据view截图, 指定范围
+ (UIImage*)cutView:(UIView*)view size:(CGSize)size
{
    UIGraphicsBeginImageContextWithOptions(size, YES, 1);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end
